/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static java.lang.Boolean.FALSE;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.filechooser.FileNameExtensionFilter;

/**
 *
 * @author BTC
 */
public final class FileWindow extends JFrame {

    private MainWindow mainWindow;
    private FileWindow fileWindow;
    private JProgressBar progressBar;
    private JButton importButton;
    private JButton exportButton;
    private JPanel progressBarsPanel;
    private List<JProgressBar> progressBars;

    private ImportHandler importHandler;
    private ExportHandler exportHandler;

    private JFileChooser fileChooser;

    private int barNumber = 0;

    private boolean importFile = FALSE;
    private boolean exportFile = FALSE;

    public FileWindow(MainWindow mainWindow) {
        super("File Manager.");
        this.mainWindow = mainWindow;
        this.fileWindow = this;
        this.progressBars = new ArrayList<JProgressBar>();

        this.importHandler = new ImportHandler();
        this.exportHandler = new ExportHandler();
        this.fileChooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("Coma separated values", "cvs");
        this.fileChooser.setFileFilter(filter);

        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        JPanel content = this.buildDisplay();
        this.setContentPane(content);
        this.pack();
        this.setVisible(true);
    }

    private JPanel buildDisplay() {
        JPanel display = new JPanel();
        display.setLayout(new BoxLayout(display, BoxLayout.Y_AXIS));
        display.setPreferredSize(new Dimension(500, 300));
        JPanel row = new JPanel();
        this.importButton = new JButton("Import from file.");
        this.importButton.addActionListener(importHandler);
        row.add(importButton);
        this.exportButton = new JButton("Export to file.");
        this.exportButton.addActionListener(exportHandler);
        row.add(exportButton);
        display.add(row);
        this.progressBarsPanel = new JPanel();        
        this.progressBarsPanel.setLayout(new BoxLayout(progressBarsPanel, BoxLayout.Y_AXIS));
        JScrollPane scroll = new JScrollPane(progressBarsPanel);
        scroll.setPreferredSize(new Dimension(500, 300));

        display.add(scroll);
        return display;
    }

    public void setProgressBar(int barNumber, int value) {
        progressBars.get(barNumber).setValue(value);
        String old = progressBars.get(barNumber).getString();
        old = old.substring(0, old.length() - 5);
        progressBars.get(barNumber).setString(old + String.format("%03d", value) + " %");

    }

    private class ImportHandler implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            int returnVal = fileChooser.showOpenDialog(FileWindow.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {

                Thread thread = new Thread(new ThreadImport(progressBars, barNumber, progressBarsPanel, fileChooser, mainWindow, fileWindow));
                thread.start();
                barNumber++;

            };

        }

    }

    private class ThreadImport implements Runnable {

        private List<JProgressBar> progressBars;
        private int barNumber;
        private JPanel progressBarsPanel;
        private JFileChooser fileChooser;
        private MainWindow mainWindow;
        private FileWindow fileWindow;

        public ThreadImport(List<JProgressBar> progressBars, int barNumber, JPanel progressBarsPanel, JFileChooser fileChooser, MainWindow mainWindow, FileWindow fileWindow) {
            this.progressBars = progressBars;
            this.barNumber = barNumber;
            this.progressBarsPanel = progressBarsPanel;
            this.fileChooser = fileChooser;
            this.mainWindow = mainWindow;
            this.fileWindow = fileWindow;

        }

        @Override
        public void run() {
            this.progressBars.add(new JProgressBar());
            this.progressBars.get(this.barNumber).setStringPainted(true);
            this.progressBars.get(this.barNumber).setValue(0);
            this.progressBars.get(this.barNumber).setString("Import " + this.fileChooser.getSelectedFile().getName() + ": " + String.format("%03d", 0) + " %");
            this.progressBarsPanel.add(this.progressBars.get(this.barNumber));
            this.progressBarsPanel.revalidate();
            this.progressBarsPanel.repaint();
            this.mainWindow.importFile(this.fileWindow, this.barNumber, this.fileChooser.getSelectedFile().getAbsolutePath());

        }

    }

    private class ExportHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int returnVal = fileChooser.showOpenDialog(FileWindow.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                Thread thread = new Thread(new ThreadExport(progressBars, barNumber, progressBarsPanel, fileChooser, mainWindow, fileWindow));
                thread.start();
                barNumber++;
            }

        }

    }

    private class ThreadExport implements Runnable {

        private List<JProgressBar> progressBars;
        private int barNumber;
        private JPanel progressBarsPanel;
        private JFileChooser fileChooser;
        private MainWindow mainWindow;
        private FileWindow fileWindow;

        public ThreadExport(List<JProgressBar> progressBars, int barNumber, JPanel progressBarsPanel, JFileChooser fileChooser, MainWindow mainWindow, FileWindow fileWindow) {
            this.progressBars = progressBars;
            this.barNumber = barNumber;
            this.progressBarsPanel = progressBarsPanel;
            this.fileChooser = fileChooser;
            this.mainWindow = mainWindow;
            this.fileWindow = fileWindow;

        }

        @Override
        public void run() {
            this.progressBars.add(new JProgressBar());
            this.progressBars.get(this.barNumber).setStringPainted(true);
            this.progressBars.get(this.barNumber).setValue(0);
            this.progressBars.get(this.barNumber).setString("Export " + this.fileChooser.getSelectedFile().getName() + ": " + String.format("%03d", 0) + " %");
            this.progressBarsPanel.add(this.progressBars.get(this.barNumber));
            this.progressBarsPanel.revalidate();
            this.progressBarsPanel.repaint();
            mainWindow.exportFile(fileWindow, barNumber, fileChooser.getSelectedFile().getAbsolutePath());
        }

    }

}
